//-----------------------------------------------------------------------------
//  Copyright (C) 2005-2008 Thomas S. Ullrich 
//
//  This file is part of the Solitaire Mahjong project.
//
//  This file may be used under the terms of the GNU General Public License.
//  This project is free software; you can redistribute it and/or modify it
//  under the terms of the GNU General Public License.
//  
//  Author: Thomas S. Ullrich
//  Last update: June 1, 2007
//-----------------------------------------------------------------------------
#ifndef smMainWindow_h
#define smMainWindow_h

#include <stack>
#include <ctime>
#include <map>
#include <QMainWindow>
#include <QLabel>
#include <QCloseEvent>
#include "smBoardView.h"
#include "smPreferencesDialog.h"
#include "smAboutDialog.h"
#include "smHelpBrowser.h"
#include "smTileItem.h"
#include "smTileFactory.h"
#include "smTileLayout.h"
#include "smHighscoreList.h"
#include "smPlayedTilesList.h"
#include "ui_smMainWindow.h"

using namespace std;

class smMainWindow : public QMainWindow
{
    Q_OBJECT

public:
    smMainWindow();
    ~smMainWindow();

    void itemSelected(smTileItem*);
    void itemsSelectedForRemoval();
    int  numberOfItemsSelected() const;
    bool itemsMatch(const smTileItem*, const smTileItem*) const;
    
public slots:
    void newGame();
    void newGameFromFile();
    void saveGameToFile();
    bool loadGameFromFile(multimap<int, int>&);
    void hintNextMove();
    void undoMove();
    void replay();
    void showPreferences();
    void showAboutWindow();
    void showHelpBrowser();
    void showHighscoreList();
    void showPlayedTiles();
    void computerVersusComputer();
    void searchSolution();
    void analyzeLayout();
    
private:
    void createStatusBar();
    void startGame(bool = false);
    void endOfGame();
    void saveSolution(stack<pair<smTileItem*, smTileItem*> >&);
    void addSelectedItem(smTileItem*);
    void removeSelectedItem(smTileItem*);
    bool selectedItemsMatch() const;
    pair<smTileItem*, smTileItem*> hint(int* = 0) const;
    
private:
    Ui::smMainWindow     ui;
    smBoardView          *mBoardView;
	smPreferencesDialog  *mPreferencesDialog;
	smAboutDialog        *mAboutDialog;
	smHelpBrowser        *mHelpBrowser;
    smHighscoreList      *mHighscoreList;
    smPlayedTilesList    *mPlayedTilesList;

    bool            mGameIsOn;
    bool            mShowMessageAtEndOfGame;
    int             mCurrentNumberOfTiles;
    int             mCurrentNumberOfUndos;
    int             mCurrentNumberOfHints;
    time_t          mStartTime;
    time_t          mEndTime;
    smTileItem      *mFirstSelected;
    smTileItem      *mSecondSelected;
    smTileFactory   *mTileFactory;
    smTileLayout    *mTileLayout;
    QLabel          *mTileCountLabel;
    QLabel          *mUndoHintCountLabel;
    QLabel          *mMessageLabel;
    stack<pair<smTileItem*, smTileItem*> > mHistoryStack;
    
};

inline int smMainWindow::numberOfItemsSelected() const
{
    int n =  0;
    if (mFirstSelected) n++;
    if (mSecondSelected) n++;
    return n;
}

inline void smMainWindow::addSelectedItem(smTileItem* item)
{
    if (item) {
        if (!mFirstSelected)
            mFirstSelected = item;
        else if (!mSecondSelected)
            mSecondSelected = item;
    }
}

inline void smMainWindow::removeSelectedItem(smTileItem* item)
{
    if (item) {
        if (mFirstSelected == item) {
            mFirstSelected = mSecondSelected;
            mSecondSelected = 0;
        }
        else if (mSecondSelected == item)
            mSecondSelected = 0;
    }
}

inline bool smMainWindow::selectedItemsMatch() const
{
    return itemsMatch(mFirstSelected, mSecondSelected);
}

inline bool smMainWindow::itemsMatch(const smTileItem* tile1, const smTileItem* tile2) const
{
    //
    //  Determin if the two selected items match
    //
    if (tile1->id() == tile2->id()) return true;
    
    //
    //  Special case for seasons and flowers
    //
    if (tile1->id() >= Season_1 && tile1->id() <= Season_4 &&
        tile2->id() >= Season_1 && tile2->id() <= Season_4) return true;
    if (tile1->id() >= Flower_1 && tile1->id() <= Flower_4 &&
        tile2->id() >= Flower_1 && tile2->id() <= Flower_4) return true;
    
    return false;
}
#endif
